#include "espcrypto.h"
#include "Base64_1.h"
#include "mbedtls/aes.h"
#include "Cipher1.h"
#include "base64.h"
Cipher1 *cipher = new Cipher1();
#define USE_SERIAL Serial

unsigned char key[32] = "xiongbin18827603142xiongbin1314";

mbedtls_aes_context aesOutgoing;

void setupCrypto()
{
  key[31] = '8'; // we replace the 32th (index 31) which contains '/0' with the '8' char.
}

void encrypt(byte plaintext[], byte encrypt[])
{
//  aes128.encryptBlock(encrypt, plaintext); // cypher->output block and plaintext->input block
}

void decrypt(byte decryptedtext[], uint8_t size)
{
  Serial.print("decryptedtext");
}

String encryptBase64(char *input)
{
  Serial.println("input size->");
  Serial.println(strlen(input));
  Serial.println(input);
  unsigned char encryptOutput[16] = {'x'};
  memset(encryptOutput, 'x', sizeof(encryptOutput));
  mbedtls_aes_init(&aesOutgoing);
  mbedtls_aes_setkey_enc(&aesOutgoing, key, 256);
  int encryptAttempt = mbedtls_aes_crypt_ecb(&aesOutgoing, MBEDTLS_AES_ENCRYPT, (const unsigned char *)input, encryptOutput);
  USE_SERIAL.println();
  USE_SERIAL.println("MBEDTLS_AES_EBC encryption result:\t ");
  USE_SERIAL.print("encryption success!"); // 0 means that the encrypt/decrypt function was successful
  USE_SERIAL.println();
  mbedtls_aes_free(&aesOutgoing);
  int encryptSize = sizeof(encryptOutput) / sizeof(const unsigned char);
  USE_SERIAL.println("Size of AES encrypted output: ");
  USE_SERIAL.println(encryptSize);
  // Base 64 Encrypt
  int inputStringLength = sizeof(encryptOutput);
  int encodedLength = Base64.decodedLength((char *)encryptOutput, inputStringLength);
  char encodedCharArray[encodedLength];
  Base64.encode(encodedCharArray, (char *)encryptOutput, inputStringLength);
  // Send to server
  USE_SERIAL.println("encodedCharArray  end...");
  USE_SERIAL.println(encodedCharArray);
  return encodedCharArray;
}

void test3()
{
  // AES Encrypt
  // esp_aes_context aesOutgoing;
  mbedtls_aes_context aesOutgoing;
  unsigned char key[32] = "1234567812345678123456781234567";
  key[31] = '8'; // we replace the 32th (index 31) which contains '/0' with the '8' char.
  char *input = "Tech tutorials x";
  unsigned char encryptOutput[16];
  mbedtls_aes_init(&aesOutgoing);
  mbedtls_aes_setkey_enc(&aesOutgoing, key, 256);
  int encryptAttempt = mbedtls_aes_crypt_ecb(&aesOutgoing, MBEDTLS_AES_ENCRYPT, (const unsigned char *)input, encryptOutput);
  USE_SERIAL.println();
  USE_SERIAL.println("MBEDTLS_AES_EBC encryption result:\t ");
  USE_SERIAL.print(encryptAttempt); // 0 means that the encrypt/decrypt function was successful
  USE_SERIAL.println();
  mbedtls_aes_free(&aesOutgoing);
  int encryptSize = sizeof(encryptOutput) / sizeof(const unsigned char);
  USE_SERIAL.println("Size of AES encrypted output: ");
  USE_SERIAL.println(encryptSize);
  // Base 64 Encrypt
  int inputStringLength = sizeof(encryptOutput);
  int encodedLength = Base64.decodedLength((char *)encryptOutput, inputStringLength);
  char encodedCharArray[encodedLength];
  Base64.encode(encodedCharArray, (char *)encryptOutput, inputStringLength);
  // Send to server
  USE_SERIAL.println("Sending to server.");
  USE_SERIAL.println(encodedCharArray);
}

String encryptBase64(String input)
{
  char *key_ptr = (char *)key;
  cipher->setKey(key_ptr);
  String s = cipher->encryptString(input);
  String bs = base64::encode(s);
  return bs;
}

void test1()
{
  char *key_ptr = (char *)key;
  cipher->setKey(key_ptr);
  String s = cipher->encryptString("woshixiong123456678kkkkkxmxjsjsfkfk274728;;.//.,,,'[][]-:;\"");

  String bs = base64::encode(s);
  Serial.println("dddddsdsfsfsf");
  Serial.println(bs);

}
